Floor and ceiling functions

Floor and ceiling functions

Floor function
Ceiling function

In mathematics and computer science, the floor and ceiling functions map a real number to the largest previous or the smallest following integer, respectively. More precisely, floor(x) = ⌊x⌋ is the largest integer not greater than x and ceiling(x) = ⌈x⌉ is the smallest integer not less than x.[1]

Contents

Notation

Gauss introduced the square bracket notation [x] for the floor function in his third proof of quadratic reciprocity (1808).[2] This remained the standard[3] in mathematics until Iverson introduced the names "floor" and "ceiling" and the corresponding notations ⌊x⌋ and ⌈x⌉ in his 1962 book A Programming Language.[4][5] Both notations are now used in mathematics; this article follows Iverson.[6]

The floor function is also called the greatest integer or entier (French for "integer") function, and its value at x the integral part or integer part of x (for negative values of x the latter terms are sometimes instead taken to be the value of the ceiling function, i.e., the value of x rounded to an integer towards 0). The language APL (programming language) uses ⌊x; other computer languages commonly use notations like entier(x) (Algol), INT(x) (BASIC), or floor(x)(C and C++).[7] In mathematics, it can also be written with boldface or double brackets [\![x]\!].[8]

The ceiling function is usually denoted by ceil(x) or ceiling(x) in non-APL computer languages that have a notation for this function. In mathematics, there is another notation with reversed boldface or double brackets ]\!]x[\![ or just using normal reversed brackets ]x[.[9]

The fractional part sawtooth function, denoted by \{x\} for real x, is defined by the formula[10]

\{x\} = x -\lfloor x\rfloor.

For all x,

0\le\{x\}<1.\;

Examples

Sample value Floor \lfloor\;\rfloor Ceiling \lceil\;\rceil Fractional part  \{ \; \}
12/5 = 2.4 2 3 2/5 = 0.4
2.7 2 3 0.7
−2.7 −3 −2 0.3
−2 −2 −2 0

See below for the definition of the fractional part function.

Definition and properties

In the following formulas, x and y are real numbers, k, m, and n are integers, and \mathbb{Z} is the set of integers (positive, negative, and zero).

Floor and ceiling may be defined by the set equations

 \lfloor x \rfloor=\max\, \{m\in\mathbb{Z}\mid m\le x\},
 \lceil x \rceil=\min\,\{n\in\mathbb{Z}\mid n\ge x\}.

Since there is exactly one integer in a half-open interval of length one, for any real x there are unique integers m and n satisfying

x-1<m\le x \le n <x+1.\;

Then  \lfloor x \rfloor = m\;  and  \;\lceil x \rceil = n\;  may also be taken as the definition of floor and ceiling.

Equivalences

These formulas can be used to simplify expressions involving floors and ceilings.[11]


\begin{align}
\lfloor x \rfloor = m &\;\;\mbox{ if and only if } &m &\le x < m+1,\\
\lceil x \rceil = n &\;\;\mbox{ if and only if } &n -1 &< x \le n,\\

\lfloor x \rfloor = m &\;\;\mbox{ if and only if } &x-1 &< m \le x,\\
\lceil x \rceil = n &\;\;\mbox{ if and only if } &x &\le n < x+1.
\end{align}

In the language of order theory, the floor function is a residuated mapping, that is, part of a Galois connection: it is the upper adjoint of the function that embeds the integers into the reals.


\begin{align}
x<n &\;\;\mbox{ if and only if } &\lfloor x \rfloor &< n, \\
n<x &\;\;\mbox{ if and only if } &n &< \lceil x \rceil, \\
x\le n &\;\;\mbox{ if and only if } &\lceil  x \rceil &\le n, \\
n\le x &\;\;\mbox{ if and only if } &n &\le \lfloor x \rfloor.
\end{align}

These formulas show how adding integers to the arguments affect the functions:


\begin{align}
\lfloor x+n \rfloor &= \lfloor x \rfloor+n,\\
\lceil x+n \rceil &= \lceil x \rceil+n,\\
\{ x+n \} &= \{ x \}.
\end{align}

The above are not necessarily true if n is not an integer; however:

\begin{align}
&\lfloor x \rfloor + \lfloor y \rfloor &\leq \;\lfloor x + y \rfloor \;&\leq\; \lfloor x \rfloor + \lfloor y \rfloor + 1,\\
&\lceil x \rceil + \lceil y \rceil -1 &\leq \;\lceil x + y \rceil \;&\leq \;\lceil x \rceil + \lceil y \rceil.
\end{align}

Relations among the functions

It is clear from the definitions that

\lfloor x \rfloor \le \lceil x \rceil,   with equality if and only if x is an integer, i.e.
\lceil x \rceil - \lfloor x \rfloor = \begin{cases}
0&\mbox{ if } x\in \mathbb{Z}\\
1&\mbox{ if } x\not\in \mathbb{Z}
\end{cases}

In fact, since for integers n:

\lfloor n \rfloor = \lceil n \rceil = n.

Negating the argument switches floor and ceiling and changes the sign:

\lfloor x \rfloor +\lceil -x \rceil=0,   i.e.
\lfloor x \rfloor + \lfloor -x \rfloor = \begin{cases}
0&\mbox{ if } x\in \mathbb{Z}\\
-1&\mbox{ if } x\not\in \mathbb{Z},
\end{cases}
\lceil x \rceil + \lceil -x \rceil = \begin{cases}
0&\mbox{ if } x\in \mathbb{Z}\\
1&\mbox{ if } x\not\in \mathbb{Z}.
\end{cases}

Negating the argument complements the fractional part:

\{ x \} +  \{ -x \} = \begin{cases}
0&\mbox{ if } x\in \mathbb{Z}\\
1&\mbox{ if } x\not\in \mathbb{Z}.
\end{cases}

The floor, ceiling, and fractional part functions are idempotent:


\begin{align}
\Big\lfloor \lfloor x \rfloor \Big\rfloor &= \lfloor x \rfloor, \\
\Big\lceil \lceil x \rceil \Big\rceil &= \lceil x \rceil, \\
\Big\{ \{ x \} \Big\} &= \{ x \}. \\
\end{align}

The result of nested floor or ceiling functions is the innermost function:


\begin{align}
\Big\lfloor \lceil x \rceil \Big\rfloor &= \lceil x \rceil, \\
\Big\lceil \lfloor x \rfloor \Big\rceil &= \lfloor x \rfloor. \\
\end{align}

For fixed y, x mod y is idempotent:

(x \,\bmod\, y) \,\bmod\, y =  x \,\bmod\, y.\;

Also, from the definitions,

\{x\}= x \,\bmod\, 1.\;

Quotients

If n ≠ 0,

0 \le \left \{\frac{m}{n} \right\} \le 1-\frac{1}{|n|}.

If n is positive[12]

\left\lfloor\frac{x+m}{n}\right\rfloor = \left\lfloor\frac{\lfloor x\rfloor +m}{n}\right\rfloor,
\left\lceil\frac{x+m}{n}\right\rceil = \left\lceil\frac{\lceil x\rceil +m}{n}\right\rceil.

If m is positive[13]

n=\left\lceil\frac{n}{m}\right\rceil + \left\lceil\frac{n-1}{m}\right\rceil +\dots+\left\lceil\frac{n-m+1}{m}\right\rceil,
n=\left\lfloor\frac{n}{m}\right\rfloor + \left\lfloor\frac{n+1}{m}\right\rfloor +\dots+\left\lfloor\frac{n+m-1}{m}\right\rfloor.

For m = 2 these imply

n= \left\lfloor \frac{n}{2}\right \rfloor + \left\lceil\frac{n}{2}\right \rceil.

More generally,[14] for positive m

\lceil mx \rceil =\left\lceil x\right\rceil  + \left\lceil x-\frac{1}{m}\right\rceil +\dots+\left\lceil x-\frac{m-1}{m}\right\rceil,
\lfloor mx \rfloor=\left\lfloor x\right\rfloor + \left\lfloor x+\frac{1}{m}\right\rfloor +\dots+\left\lfloor x+\frac{m-1}{m}\right\rfloor.

The following can be used to convert floors to ceilings and vice-versa (m positive)[15]

\left\lceil \frac{n}{m} \right\rceil = \left\lfloor \frac{n+m-1}{m} \right\rfloor = \left\lfloor \frac{n - 1}{m} \right\rfloor + 1,
\left\lfloor \frac{n}{m} \right\rfloor = \left\lceil \frac{n-m+1}{m} \right\rceil = \left\lceil \frac{n + 1}{m} \right\rceil - 1,

If m and n are positive and coprime, then

\sum_{i=1}^{n-1} \left\lfloor \frac{im}{n} \right\rfloor = \frac{1}{2}(m - 1)(n - 1).

Since the right-hand side is symmetrical in m and n, this implies that

\left\lfloor \frac{m}{n} \right \rfloor + \left\lfloor \frac{2m}{n} \right \rfloor + \dots + \left\lfloor \frac{(n-1)m}{n} \right \rfloor = 
\left\lfloor \frac{n}{m} \right \rfloor + \left\lfloor \frac{2n}{m} \right \rfloor + \dots + \left\lfloor \frac{(m-1)n}{m} \right \rfloor.

More generally, if m and n are positive,

\begin{align}
&\left\lfloor \frac{x}{n} \right \rfloor +
\left\lfloor \frac{m+x}{n} \right \rfloor +
\left\lfloor \frac{2m+x}{n} \right \rfloor +
\dots +
\left\lfloor \frac{(n-1)m+x}{n} \right \rfloor\\= 
&\left\lfloor \frac{x}{m} \right \rfloor +
\left\lfloor \frac{n+x}{m} \right \rfloor +
\left\lfloor \frac{2n+x}{m} \right \rfloor +
\dots +
\left\lfloor \frac{(m-1)n+x}{m} \right \rfloor.
\end{align}

This is sometimes called a reciprocity law.[16]

Nested divisions

For positive integers m,n, and arbitrary real number x:

 \left\lfloor \frac{\lfloor x/m\rfloor}{n} \right\rfloor = \left\lfloor \frac{x}{mn} \right\rfloor
 \left\lceil \frac{\lceil x/m\rceil}{n} \right\rceil = \left\lceil \frac{x}{mn} \right\rceil

Continuity

None of the functions discussed in this article are continuous, but all are piecewise linear. \lfloor x \rfloor  and \lceil x \rceil are piecewise constant functions, with discontinuites at the integers. \{ x\} also has discontinuites at the integers, and   x  \,\bmod\, y as a function of x for fixed y is discontinuous at multiples of y.

\lfloor x \rfloor  is upper semi-continuous and  \lceil x \rceil  and \{ x\}\;  are lower semi-continuous. x mod y is lower semicontinuous for positive y and upper semi-continuous for negative y.

Series expansions

Since none of the functions discussed in this article are continuous, none of them have a power series expansion. Since floor and ceiling are not periodic, they do not have Fourier series expansions.

x mod y for fixed y has the Fourier series expansion[17]

x \,\bmod\, y = \frac{y}{2} - \frac{y}{\pi} \sum_{k=1}^\infty
\frac{\sin\left(\frac{2 \pi k x}{y}\right)} {k}\qquad\mbox{for }x\mbox{ not a multiple of }y.

in particular {x} = x mod 1 is given by

\{x\}= \frac{1}{2} - \frac{1}{\pi} \sum_{k=1}^\infty
\frac{\sin(2 \pi k x)} {k}\qquad\mbox{for }x\mbox{ not an integer}.

At points of discontinuity, a Fourier series converges to a value that is the average of its limits on the left and the right, unlike the floor, ceiling and fractional part functions: for y fixed and x a multiple of y the Fourier series given converges to y/2, rather than to x mod y = 0. At points of continuity the series converges to the true value.

Using the formula {x} = x − floor(x), floor(x) = x − {x} gives

\lfloor x\rfloor = x - \frac{1}{2} + \frac{1}{\pi} \sum_{k=1}^\infty \frac{\sin(2 \pi k x)}{k}\qquad\mbox{for }x\mbox{ not an integer}.

Applications

mod operator

The mod operator, denoted by x mod y for real x and y, y ≠ 0, is defined by the formula

x \,\bmod\, y = x-y\left\lfloor \frac{x}{y}\right\rfloor.

x mod y is always between 0 and y; i.e.

if y is positive,

0 \le x \,\bmod\, y <y,

and if y is negative,

0 \ge x \,\bmod\, y >y.

If x is an integer and y is a positive integer,

(x \,\bmod\, y) \equiv x \pmod{y}.

x mod y for a fixed y is a sawtooth function.

Quadratic reciprocity

Gauss's third proof of quadratic reciprocity, as modified by Eisenstein, has two basic steps.[18][19]

Let p and q be distinct positive odd prime numbers, and let

m = \frac{p - 1}{2},\;\; n = \frac{q - 1}{2}.

First, Gauss's lemma is used to show that the Legendre symbols are given by

\left(\frac{q}{p}\right) = (-1)^{\left\lfloor\frac{q}{p}\right\rfloor +\left\lfloor\frac{2q}{p}\right\rfloor +\dots +\left\lfloor\frac{mq}{p}\right\rfloor }

and

\left(\frac{p}{q}\right) = (-1)^{\left\lfloor\frac{p}{q}\right\rfloor +\left\lfloor\frac{2p}{q}\right\rfloor +\dots +\left\lfloor\frac{np}{q}\right\rfloor }.

The second step is to use a geometric argument to show that

\left\lfloor\frac{q}{p}\right\rfloor +\left\lfloor\frac{2q}{p}\right\rfloor +\dots +\left\lfloor\frac{mq}{p}\right\rfloor 

+\left\lfloor\frac{p}{q}\right\rfloor +\left\lfloor\frac{2p}{q}\right\rfloor +\dots +\left\lfloor\frac{np}{q}\right\rfloor 

= mn.

Combining these formulas gives quadratic reciprocity in the form

\left(\frac{p}{q}\right) \left(\frac{q}{p}\right) = (-1)^{mn}=(-1)^{\frac{p-1}{2}\frac{q-1}{2}}.

There are formulas that use floor to express the quadratic character of small numbers mod odd primes p:[20]

\left(\frac{2}{p}\right)  = (-1)^{\left\lfloor\frac{p+1}{4}\right\rfloor},
\left(\frac{3}{p}\right)  = (-1)^{\left\lfloor\frac{p+1}{6}\right\rfloor}.

Rounding

The ordinary rounding of the positive number x to the nearest integer can be expressed as \lfloor x + 0.5\rfloor. The ordinary rounding of the negative number x to the nearest integer can be expressed as \lceil x - 0.5\rceil.

Truncation

The truncation of a nonnegative integer is given by \lfloor x\rfloor. The truncation of a nonpositive integer is given by \lceil x - 1\rceil + 1.

The truncation of any real number can be given by: \sgn(x) \lfloor |x| \rfloor, where sgn(x) is the sign function.

Number of digits

The number of digits in base b of a positive integer k is

\lfloor \log_{b}{k} \rfloor + 1.

Factors of factorials

Let n be a positive integer and p a positive prime number. The exponent of the highest power of p that divides n! is given by the formula[21]

\left\lfloor\frac{n}{p}\right\rfloor + \left\lfloor\frac{n}{p^2}\right\rfloor + \left\lfloor\frac{n}{p^3}\right\rfloor + \dots

Note that this is a finite sum, since the floors are zero when pk > n.

Beatty sequence

Beatty sequence shows how every positive irrational number gives rise to a partition of the natural numbers into two sequences via the floor function.[22]

Euler's constant γ

There are formulas for Euler's constant γ = 0.57721 56649 ... that involve the floor and ceiling, e.g.[23]

\gamma =\int_1^\infty\left({1\over\lfloor x\rfloor}-{1\over x}\right)\,dx,
 \gamma =      \lim_{n \to \infty} \frac{1}{n}\, \sum_{k=1}^n \left ( \left \lceil \frac{n}{k} \right \rceil - \frac{n}{k} \right ),

and


 \gamma = \sum_{k=2}^\infty (-1)^k \frac{ \left \lfloor \log_2 k \right \rfloor}{k}
  = \tfrac12-\tfrac13
  + 2\left(\tfrac14 - \tfrac15 + \tfrac16 - \tfrac17\right)
  + 3\left(\tfrac18 - \dots - \tfrac1{15}\right) + \dots

Riemann ζ function

The fractional part function also shows up in integral representations of the Riemann zeta function. It is straightforward to prove (using integration by parts)[24] that if φ(x) is any function with a continuous derivative in the closed interval [a, b],

{ \sum_{a<n\le b}\phi(n) = 
\int_a^b\phi(x) dx +
\int_a^b\left(\{x\}-\tfrac12\right)\phi'(x) dx +
\left(\{a\}-\tfrac12\right)\phi(a) -
\left(\{b\}-\tfrac12\right)\phi(b). }

Letting φ(n) = n−s for real part of s greater than 1 and letting a and b be integers, and letting b approach infinity gives

\zeta(s) = s\int_1^\infty\frac{\frac12-\{x\}}{x^{s+1}}\;dx + \frac{1}{s-1} + \frac12.

This formula is valid for all s with real part greater than −1, (except s = 1, where there is a pole) and combined with the Fourier expansion for {x} can be used to extend the zeta function to the entire complex plane and to prove its functional equation.[25]

For s = σ + i t in the critical strip (i.e. 0 < σ < 1),

\zeta(s)=s\int_{-\infty}^\infty e^{-\sigma\omega}(\lfloor e^\omega\rfloor - e^\omega)e^{-it\omega}\,d\omega.

In 1947 van der Pol used this representation to construct an analogue computer for finding roots of the zeta function.[26]

Formulas for prime numbers

n is a prime if and only if[27]


\sum_{m=1}^\infty \left(\left\lfloor\frac{n}{m}\right\rfloor-\left\lfloor\frac{n-1}{m}\right\rfloor\right) = 2.

Let r > 1 be an integer, pn be the nth prime, and define

\alpha = \sum_{m=1}^\infty p_m r^{-m^2}.

Then[28]

p_n = \left\lfloor r^{n^2}\alpha \right\rfloor - r^{2n-1}\left\lfloor r^{(n-1)^2}\alpha\right\rfloor.

There is a number θ = 1.3064... (Mill's constant) with the property that

\left\lfloor \theta^3 \right\rfloor, \left\lfloor \theta^9 \right\rfloor, \left\lfloor \theta^{27} \right\rfloor, \dots

are all prime.[29]

There is also a number ω = 1.9287800... with the property that

\left\lfloor 2^\omega\right\rfloor, \left\lfloor 2^{2^\omega} \right\rfloor, \left\lfloor 2^{2^{2^\omega}} \right\rfloor, \dots

are all prime.[29]

π(x) is the number of primes less than or equal to x. It is a straightforward deduction from Wilson's theorem that[30]

\pi(n) = \sum_{j=2}^n\left\lfloor\frac{(j-1)!+1}{j} - \left\lfloor\frac{(j-1)!}{j}\right\rfloor\right\rfloor.

Also, if n ≥ 2,[31]


\pi(n) = \sum_{j=2}^n \left\lfloor \frac{1}{\sum_{k=2}^j\left\lfloor\left\lfloor\frac{j}{k}\right\rfloor\frac{k}{j}\right\rfloor}\right\rfloor.

None of the formulas in this section is of any practical use.

Solved problem

Ramanujan submitted this problem to the Journal of the Indian Mathematical Society.[32]

If n is a positive integer, prove that

(i)     \left\lfloor\tfrac{n}{3}\right\rfloor + \left\lfloor\tfrac{n+2}{6}\right\rfloor + \left\lfloor\tfrac{n+4}{6}\right\rfloor = \left\lfloor\tfrac{n}{2}\right\rfloor + \left\lfloor\tfrac{n+3}{6}\right\rfloor,

(ii)     \left\lfloor\tfrac12 + \sqrt{n+\tfrac12}\right\rfloor = \left\lfloor\tfrac12 + \sqrt{n+\tfrac14}\right\rfloor,

(iii)     \left\lfloor\sqrt{n}+ \sqrt{n+1}\right\rfloor = \left\lfloor \sqrt{4n+2}\right\rfloor.

Unsolved problem

The study of Waring's problem has led to an unsolved problem:

Are there any positive integers k, k ≥ 6, such that[33]

3^k-2^k\left\lfloor \left(\tfrac32\right)^k \right\rfloor  > 2^k-\left\lfloor \left(\tfrac32\right)^k \right\rfloor -2\;\;?

Mahler[34] has proved there can only be a finite number of such k; none are known.

Computer implementations

C and C++

Int function from floating-point conversion

The C, C++, and related programming languages have standard functions floor()[35] and ceil().[36] In these languages, conversion of floating point values to integers using type casting syntax truncates toward zero.[37]

Python

Python follows the standard mathematical definition with integer division and its math.floor and ceil function, giving a result heading towards negative infinity in all cases.[38] Python 3.0 doesn't have integer division, so the floor and ceil functions would have to be used with that version.[39]

Spreadsheet software

Most spreadsheet programs support some form of a ceiling function. Although the details differ between programs, most implementations support a second parameter—a multiple of which the given number is to be rounded to. As a typical example, ceiling(2, 3) would round 2 up to the nearest multiple of 3, so this would return 3. The definition of what "round up" means, however, differs from program to program.

Microsoft Excel's ceiling function does not follow the mathematical definition, but rather as with (int) operator in C, it is a mixture of the floor and ceiling function: for x ≥ 0 it returns floor(x), and for x < 0 it returns ceiling(x). This has followed through to the Office Open XML file format. For example, CEILING(-4.5) returns −5. A mathematical ceiling function can be emulated in Excel by using the formula "-INT(-value)" (please note that this is not a general rule, as it depends on Excel's INT function, which behaves differently than most programming languages).

The OpenDocument file format, as used by OpenOffice.org and others, follows the mathematical definition of ceiling for its ceiling function, with an optional parameter for Excel compatibility. For example, CEILING(-4.5) returns −4.

Typesetting

The floor and ceiling function are usually typeset with left and right square brackets where the upper (for floor function) or lower (for ceiling function) horizontal bars are missing, and, e.g., in the LaTeX typesetting system these symbols can be specified with the \lfloor, \rfloor, \lceil and \rceil commands in math mode. HTML 4.0 uses the same names: &lfloor;, &rfloor;, &lceil;, and &rceil;. Unicode contains codepoints for these symbols at U+2308U+230B: ⌈x⌉, ⌊x⌋.

See also

Notes

  1. Graham, Knuth, & Patashnik, Ch. 3.1
  2. Lemmermeyer, pp. 10, 23.
  3. e.g. Cassels, Hardy & Wright, and Ribenboim use Gauss's notation, Graham, Knuth & Patashnik, and Crandall & Pomerance use Iverson's.
  4. Iverson, p. 12.
  5. Higham, p. 25.
  6. See the Wolfram MathWorld article.
  7. Sullivan, p. 86.
  8. Mathwords: Floor Function.
  9. Mathwords: Ceiling Function
  10. Graham, Knuth, & Patashnik, p. 70.
  11. Graham, Knuth, & Patashink, Ch. 3
  12. Graham, Knuth, & Patashnik, p. 72
  13. Graham, Knuth, & Patashnik, p. 85
  14. Graham, Knuth, & Patashnik, p. 85 and Ex. 3.15
  15. Graham, Knuth, & Patashnik, Ex. 3.12
  16. Graham, Knuth, & Patashnik, p. 94
  17. Titchmarsh, p. 15, Eq. 2.1.7
  18. Lemmermeyer, § 1.4, Ex. 1.32–1.33
  19. Hardy & Wright, §§ 6.11–6.13
  20. Lemmermeyer, p. 25
  21. Hardy & Wright, Th. 416
  22. Graham, Knuth, & Patashnik, pp. 77–78
  23. These formulas are from the Wikipedia article Euler's constant, which has many more.
  24. Titchmarsh, p. 13
  25. Titchmarsh, pp.14–15
  26. Crandall & Pomerance, p. 391
  27. Crandall & Pomerance, Ex. 1.3, p. 46
  28. Hardy & Wright, § 22.3
  29. 29.0 29.1 Ribenboim, p. 186
  30. Ribenboim, p. 181
  31. Crandall & Pomerance, Ex. 1.4, p. 46
  32. Ramanujan, Question 723, Papers p. 332
  33. Hardy & Wright, p. 337
  34. Mahler, K. On the fractional parts of the powers of a rational number II, 1957, Mathematika, 4, pages 122-124
  35. http://www.cplusplus.com/reference/clibrary/cmath/floor.html
  36. http://www.cplusplus.com/reference/clibrary/cmath/ceil.html
  37. ISO standard for C, § 6.3.1.4, p. 43.
  38. http://docs.python.org/library/math.html
  39. http://www.python.org/dev/peps/pep-0238/

References

External links